<template>
  <div class="update-name">
    <van-nav-bar
      title="更新昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="finished"
    />
    <div class="field-wrap">
      <van-field
        v-model="localName"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/apis/user'
export default {
  name: 'UpdateName',
  props: {
    name: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      localName: this.name
    }
  },
  methods: {
    async finished () {
      this.$toast.loading({
        message: '保存中',
        forbidClick: true
      })
      await updateUserProfile({
        name: this.localName
      })
      this.$toast.success('保存成功')
      this.$emit('update:name', this.localName)
      this.$emit('close')
    }
  }
}
</script>

<style lang="less" scoped>
.field-wrap{
  padding: 10px;
}
</style>
